package com.zhs.gccs.view.gt;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.model.FieldConfig;
import com.zhs.gccs.model.GtLb;
import com.zhs.gccs.view.BaseView;
import com.zhs.gccs.vo.SysDictBean;

import java.util.ArrayList;
import java.util.List;

public class GtPropSelectView extends BaseView {
    private TextView tv_cancel;
    private TextView tv_confirm;
    private View ll_cb_all;
    private CheckBox cb_selected;
    private RecyclerView rv_items;
    private List<GtLb> gtLbs;
    private GtLbGroupAdapter adapter;

    public GtPropSelectView(@NonNull Context context) {
        super(context);
    }

    public GtPropSelectView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public GtPropSelectView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_gt_prop,
                this, true);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        ll_cb_all = view.findViewById(R.id.ll_cb_all);
        cb_selected = view.findViewById(R.id.cb_selected);
        rv_items = view.findViewById(R.id.rv_items);
        tv_cancel.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Stream.of(gtLbs).forEach(item->item.setChecked(false));
                cb_selected.setChecked(false);
                adapter.notifyDataSetChanged();
//                popupWindow.dismiss();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                if (dataChangeListener != null) dataChangeListener.dataChange();
                if (dataChangeListener != null) dataChangeListener.dismis();
            }
        });
        ll_cb_all.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                cb_selected.setChecked(!cb_selected.isChecked());
                Stream.of(gtLbs).forEach(item -> item.setChecked(cb_selected.isChecked()));
                adapter.notifyDataSetChanged();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        this.gtLbs = new ArrayList<>();
//        rv_items.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_items.setLayoutManager(layoutManager);
        adapter = new GtLbGroupAdapter(context, gtLbs);
        rv_items.addItemDecoration(new DividerItemDecoration(context
                , DividerItemDecoration.VERTICAL));
        rv_items.setAdapter(adapter);
        adapter.setOperationListener(new GtLbGroupAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                GtLb checkBoxModel = gtLbs.get(position);
                checkBoxModel.setChecked(!checkBoxModel.isChecked());
                adapter.notifyItemChanged(position);
                long count = Stream.of(gtLbs).filter(i -> i.isChecked()).count();
                boolean allSelected = count==gtLbs.size()?true:false;
                cb_selected.setChecked(allSelected);
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
    }

    private List<SysDictBean.SysDictItem> gtProps;
    public void initData(List<SysDictBean.SysDictItem> data) {
        this.gtProps = data;
//        for(SysDictBean.SysDictItem dict:data){
//            CheckBoxModel lb = new CheckBoxModel(dict.getDictLabel(),dict.getDictValue(),false);
//            gtLbs.add(lb);
//        }
//        adapter.notifyDataSetChanged();
        gtLbs.clear();
        for(SysDictBean.SysDictItem dict:data){
            GtLb lb = new GtLb();
            lb.setName(dict.getDictLabel());
            FieldConfig lbConfig = JSON.parseObject(dict.getRemark(), FieldConfig.class);
//            lb.setCode(dict.getRemark());//我们用备注填写字段
            lb.setChecked(lbConfig.isChecked());
            lb.setCode(lbConfig.getCode());
            lb.setSort(lbConfig.isSort());
            lb.setAsc(lbConfig.isAsc());
            lb.setFixed(lbConfig.isFixed());
            lb.setShow(lbConfig.isShow());
            lb.setType(lbConfig.getType());
            lb.setItems(JSON.parseArray(dict.getDictValue(), FieldConfig.class));//[{}]
            gtLbs.add(lb);
        }
        adapter.notifyDataSetChanged();
        long count = Stream.of(gtLbs).filter(i -> i.isChecked()).count();
        boolean allSelected = count==gtLbs.size()?true:false;
        cb_selected.setChecked(allSelected);
    }


    public List<GtLb> getSelected() {
        return Stream.of(gtLbs).filter(i -> i.isChecked()).toList();
    }

    public List<CheckBoxModel> getSelectedValue() {
        return Stream.of(gtLbs).filter(i -> i.isChecked()).map(i->new CheckBoxModel(i.getName(),i.getCode(),true)).toList();
    }

    private GbPropSelectView.DataChangeListener dataChangeListener;
    public void setDataChangeListener(GbPropSelectView.DataChangeListener dataChangeListener) {
        this.dataChangeListener = dataChangeListener;
    }

    public interface DataChangeListener{
        void  dataChange();

        void dismis();
    }
}
